﻿/*
03-树2 List Leaves (25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:
For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
*/
#pragma warning(disable:4996)
#pragma warning(disable:6031)
#pragma warning(disable:6011)

namespace Y_exam_03_2_tree_list_leaves {
int main();


#include <stdio.h>
#include <stdlib.h>

#define MaxTree 10
#define ElementType char
#define Tree int
#define Null -1
#define ERROR -1

/*队列（基于数组）*/

typedef int Position;
struct QNode {
	Tree Data[30];     /* 存储元素的数组 */
	Position Front, Rear;  /* 队列的头、尾指针 */
	int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode* Queue;

Queue CreateQueue(int MaxSize)
{
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Front = Q->Rear = 0;
	Q->MaxSize = MaxSize;
	return Q;
}

bool IsFull(Queue Q)
{
	return ((Q->Rear + 1) % Q->MaxSize == Q->Front);
}

bool AddQ(Queue Q, Tree X)
{
	if (IsFull(Q)) {
		printf("队列满");
		return false;
	}
	else {
		Q->Rear = (Q->Rear + 1) % Q->MaxSize;
		Q->Data[Q->Rear] = X;
		return true;
	}
}

bool IsEmptyQ(Queue Q)
{
	return (Q->Front == Q->Rear);
}

Tree DeleteQ(Queue Q)
{
	if (IsEmptyQ(Q)) {
		printf("队列空");
		return ERROR;
	}
	else {
		Q->Front = (Q->Front + 1) % Q->MaxSize;
		return  Q->Data[Q->Front];
	}
}

struct TreeNode
{
	Tree Left;
	Tree Right;
}T1[MaxTree];

int g_first = 1;

Tree BuildTree(struct TreeNode T[])
{
	int i, N;
	int check[MaxTree] = { 0 };
	char cl, cr;
	Tree Root = Null;
	scanf("%d\n", &N);

	if (N > 0) {
		for (i = 0; i < N; ++i) {
			scanf("%c %c\n",  &cl, &cr);
			if (cl != '-') {
				T[i].Left = cl - '0';
				check[T[i].Left] = 1;
			}
			else
				T[i].Left = Null;
			// 对cr的对应处理
			if (cr != '-') {
				T[i].Right = cr - '0';
				check[T[i].Right] = 1;
			}
			else
				T[i].Right = Null;
		}
		for (i = 0; i < N; i++)
			if (!check[i])
				break;
		Root = i;
	}
	return Root;
}

void LevelOrderTraversal(Tree BT)
{
	Queue Q; Tree T;
	if (BT==Null) return; /* 若是空树则直接返回 */
	Q = CreateQueue(30); /* 创建并初始化队列Q*/
	AddQ(Q, BT);
	while (!IsEmptyQ(Q)) {
		T = DeleteQ(Q);
		if ((T1[T].Left == Null) && (T1[T].Right == Null))
		{
			if (g_first) {
				printf("%d", T);
				g_first = 0;
			}
			else
				printf(" %d", T);
		}
		if (T1[T].Left != Null) AddQ(Q, T1[T].Left);
		if (T1[T].Right != Null) AddQ(Q, T1[T].Right);
	}
}

void PreOrderTraversal(Tree BT)
{
	if (BT != Null) {
		PreOrderTraversal(T1[BT].Left);
		if ((T1[BT].Left == Null) && (T1[BT].Right == Null))
		{
			if (g_first) {
				printf("%d", BT);
				g_first = 0;
			}
			else
				printf(" %d", BT);
		}
		PreOrderTraversal(T1[BT].Right);
	}
}

int main()
{
	Tree R1;

	R1 = BuildTree(T1);
	LevelOrderTraversal(R1);

	return 0;
}

}

int main_Y_exam_03_2_tree_list_leaves()
{
	return Y_exam_03_2_tree_list_leaves::main();
}